package dao;

import java.sql.*;

public class UserDaoImpl {


    // psvm
    public static void main(String[] args) {
        // jdbc操作 查询用户表
        // 1、加载驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            // 2、获取连接
            String url = "jdbc:mysql://localhost:3306/store?useUnicode=true&characterEncoding=utf8";
            String name = "root";
            // ctrl+d 复制一行
            String pass = "123456";
            // connection 连接
            Connection connection = DriverManager.getConnection(url,name,pass);
            if (connection != null){
                // sout
                System.out.println("连接成功");
            }

            // 3、编写sql语句
            String sql = "select * from user;";

            // 4、预编译sql
            PreparedStatement preparedStatement = connection.prepareStatement(sql);

            // 5、执行查询
            ResultSet resultSet = preparedStatement.executeQuery();

            // 6、遍历结果  循环
            while(resultSet.next()){
                // 查询表数据  (int) 强制类型转换
                int id = (int) resultSet.getObject(1);
                String name_ = (String) resultSet.getObject(2);
                String sex = (String) resultSet.getObject(3);

                // 打印
                System.out.println(id+"----"+name_+"----"+sex);
            }


        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
